package com.anuo.app.study;

import com.anuo.app.common.entity.RowState;
import com.anuo.app.common.utils.JsonUtil;
import com.anuo.app.modules.user.entity.UserAccount;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring/spring-context.xml"})
/**
 * @ 枚举使用详解 见下面的 studyEnum 方法
 * @ 枚举的复杂使用(可以一个枚举值存储一行数据)  使用见下面main 方法
 */
public enum  StudyEnum {
    SUCCESS(100, "成功","http"),
    USERNAME_OR_PASSWORD_ERROR(-1001, "用户名或密码错误","tcp"),
    USER_NOT_FOUND(-1002, "用户不存在","user"),
    USER_NOT_LOGIN(-1003, "用户未登录","user");

    /**
     * 返回码
     */
    private int code;

    /**
     * 返回结果描述
     */
    private String message;

    /**
     * 类型
     */
    private String type;

    StudyEnum(int code, String message, String type) {
        this.code = code;
        this.message = message;
        this.type=type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    /**
     * 枚举:
     * #枚举常见定义见 RowState 枚举, 枚举在序列化和反序列化时是字符状态, 比如RowState 里面的ADDED, 序列化后
     * 就是"ADDED", 这样是易读的,效果跟 final static 常量一样.
     * #和 final static比较: 尽量用枚举,可以带来很多好处,比如编译检查等等
     * 比较参考:https://stackoverflow.com/questions/11575376/why-use-enums-instead-of-constants
     * https://stackoverflow.com/questions/9969690/whats-the-advantage-of-a-java-enum-versus-a-class-with-public-static-final-fiel
     * #枚举例子: 优质文章 https://www.mkyong.com/java/java-enum-example/
     */
    @Test
    public void studyEnum() {
        //使用实例
        RequestApi(StudyEnum.SUCCESS);
        //枚举赋值
        UserAccount userAccount = new UserAccount();
        userAccount.setRowState(RowState.DELETED);
        //枚举比较
        if (userAccount.getRowState() == RowState.ADDED) {

        }
        System.out.println(JsonUtil.toJsonPretty(userAccount));

    }

    /**
     * 取枚举值的子数据
     * @param studyEnum
     */
    public static void RequestApi(StudyEnum studyEnum){
        int code=studyEnum.getCode();
        String message = studyEnum.getMessage();
    }

}
